package cn.com.checne.controller.superManager.synch;

import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.com.checne.constants.SynchConstants;
import cn.com.checne.dao.WebsiteInfoDao;
import cn.com.checne.model.domain.WebsiteInfo;
import cn.com.checne.util.BlowFish;
import cn.com.checne.util.HttpConnection;
import cn.com.checne.util.WebUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
public class WebsiteInfoSynchController {
	protected static final Logger logger = Logger
			.getLogger(WebsiteInfoSynchController.class);

	@Resource
	private WebsiteInfoDao websiteInfoDao;

	@RequestMapping(value =SynchConstants.SYNCH_WEBSITE_REV , method =RequestMethod.POST)
	public void websiteInfoSynch(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PrintWriter out = response.getWriter();
		BlowFish bf = new BlowFish(SynchConstants.SYNCH_SECRET_KEY);
		String sign = WebUtils.getRequestParameterAsString(request, "sign");
		if (StringUtils.isBlank(sign)) {
			out.print(SynchConstants.SYNCH_RESULT_NOPWD);
			out.close();
			return;
		}
		sign = bf.decryptString(sign);
		Gson gson = new Gson();
		Type type = new TypeToken<List<WebsiteInfo>>() {}.getType();
		List<WebsiteInfo> list = gson.fromJson(sign, type);
		if (this.websiteInfoDao.batchInsert(list))
			out.print(SynchConstants.SYNCH_RESULT_OK);
		else
			out.print(SynchConstants.SYNCH_RESULT_NO);
	}

	@RequestMapping(value =SynchConstants.SYNCH_WEBSITE_SEND, method =RequestMethod.GET)
	public ModelAndView websiteInfoSynch(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		StringBuilder message = new StringBuilder();

		List<WebsiteInfo> sourceList = this.websiteInfoDao.getSubList(null, 0,
				2147483647);

		List<WebsiteInfo> needSynchWebsiteInfoList = new ArrayList<WebsiteInfo>();
		for (WebsiteInfo website : sourceList) {
			if (website.getIsSelf() == 0) {
				needSynchWebsiteInfoList.add(website);
			}

		}

		for (WebsiteInfo website : needSynchWebsiteInfoList) {
			List<WebsiteInfo> list1 = new ArrayList<WebsiteInfo>();
			for (WebsiteInfo w : sourceList) {
				w.setIsSelf(0);
				list1.add(w);
			}
			List<WebsiteInfo> list2 = new ArrayList<WebsiteInfo>();
			for (WebsiteInfo w : list1) {
				if (w.getUrl().equalsIgnoreCase(website.getUrl()))
					w.setIsSelf(1);
				else {
					w.setId(0);
				}
				list2.add(w);
			}
			Gson gson = new Gson();
			String sendJsonString = gson.toJson(list2, List.class);

			BlowFish bf = new BlowFish(SynchConstants.SYNCH_SECRET_KEY);
			String synchURL = website.getUrl() + SynchConstants.SYNCH_WEBSITE_REV;
			String synchContent = "sign=" + bf.encryptString(sendJsonString);
			logger.info("[synch-websiteInfo] [" + synchURL + "] "
					+ synchContent);

			String result = HttpConnection.sendPost(synchURL, synchContent);

			if (StringUtils.isNotBlank(result)) {
				if (StringUtils.equals(result, SynchConstants.SYNCH_RESULT_OK)) {
					message.append(website.getName() + "&nbsp;&nbsp;同步"
							+ "成功!\r\n");
				} else if (StringUtils.equals(result, SynchConstants.SYNCH_RESULT_NOPWD)) {
					message.append(website.getName()
							+ "&nbsp;&nbsp;密钥验证失败!\r\n");
				} else {
					message.append(website.getName() + "&nbsp;&nbsp;同步"
							+ "失败!\r\n");
				}
			}
		}

		ModelAndView mav = new ModelAndView("common/message");
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}
}